package http;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import library.service.Book;

public class LibraryServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private Book foundBook;

	public Book getFoundBook() {
		return foundBook;
	}

	public void setFoundBook(Book foundBook) {
		this.foundBook = foundBook;
	}

	private String printBokInfo() {
		String bookInfo = "";
		Book book = getFoundBook();
		if (book != null) {
			bookInfo = "<p/>Book Name: " + book.getName() + ", Book Category: "
					+ book.getCategory() + "<p/>";
		}
		return bookInfo;
	}

	protected void doGet(HttpServletRequest requset,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
				+ "Transitional//EN\">\n";
		out.println(docType + "<html>\n"
				+ "<head><title>Library</title></head>\n" + "<body>\n"
				+ "<h1>Library WebApp</h1>\n" + printBokInfo()
				+ "<br/>Image: <img src=\"/res/java-logo.jpg\">"
				+ "</body></html>");
	}
}
